package com.kqb.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.IdUtil;
import com.kqb.api.user.vo.UserAddrVO;
import com.kqb.common.core.utils.AssertUtils;
import com.kqb.order.mapper.OrderAddrMapper;
import com.kqb.order.mapper.OrderItemMapper;
import com.kqb.order.pojo.Order;
import com.kqb.order.mapper.OrderMapper;
import com.kqb.order.pojo.OrderAddr;
import com.kqb.order.pojo.OrderItem;
import com.kqb.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kqb.order.vo.ShopCartItemVO;
import com.kqb.order.vo.ShopCartOrderMergerVO;
import com.kqb.order.vo.ShopCartOrderVO;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单信息 服务实现类
 * </p>
 *
 * @author 杉果树皮
 * @since 2025-04-12
 */
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderAddrMapper orderAddrMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Override
    @Transactional
    public List<Long> submit(Long userId, ShopCartOrderMergerVO shopCartOrderMergerVO) {
        UserAddrVO userAddr = shopCartOrderMergerVO.getUserAddr();

        OrderAddr orderAddr = new OrderAddr();
        BeanUtil.copyProperties(userAddr, orderAddr);

        orderAddr.setUserId(userId);
        //保存用户订单地址
        int insert1 = orderAddrMapper.insert(orderAddr);
        AssertUtils.isZero(insert1, "保存用户订单地址失败");

        ArrayList<Long> orderIds = new ArrayList<>();

        //保存订单
        List<ShopCartOrderVO> shopCartOrders = shopCartOrderMergerVO.getShopCartOrders();
        shopCartOrders.forEach(e -> {
            Order order = getOrder(userId, shopCartOrderMergerVO.getDvyType(), e);
            int insert = orderMapper.insert(order);
            AssertUtils.isZero(insert1, "保存用户订单失败" + order.getUserId());
            orderIds.add(order.getOrderId());
            //保存订单商品列表
            List<ShopCartItemVO> shopCartItemVO = e.getShopCartItemVO();
            ArrayList<OrderItem> orderItems = new ArrayList<>();
            shopCartItemVO.forEach(el -> {
                OrderItem orderItem = getOrderItem(order, el);
                orderItems.add(orderItem);
            });
            List<BatchResult> insert2 = orderItemMapper.insert(orderItems);

//            AssertUtils.isZero(insert1,"保存用户订单失败"+order.getUserId());

        });
        return orderIds;
    }

    //将传过来的购物车商品列表转换成订单列表 ShopCartItemVO-》OrderItem
    private OrderItem getOrderItem(Order order, ShopCartItemVO shopCartItem) {
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(order.getOrderId());
        orderItem.setShopId(shopCartItem.getShopId());
        orderItem.setSkuId(shopCartItem.getSkuId());
        orderItem.setSpuId(shopCartItem.getSpuId());
        orderItem.setSkuName(shopCartItem.getSkuName());
        orderItem.setCount(shopCartItem.getCount());
        orderItem.setSpuName(shopCartItem.getSpuName());
        orderItem.setPic(shopCartItem.getImgUrl());
        orderItem.setPrice(shopCartItem.getSkuPriceFee());
        orderItem.setUserId(order.getUserId());
        orderItem.setSpuTotalAmount(shopCartItem.getTotalAmount());
//        orderItem.setShopCartTime();
        // 订单项支付金额
        orderItem.setSpuTotalAmount(shopCartItem.getTotalAmount());
        return orderItem;
    }

    //得到商品信息 ShopCartOrderVO -》 Order
    private Order getOrder(Long userId, Integer dvyType, ShopCartOrderVO shopCartOrderDto) {
        //获取订单id 使用雪花算法
        long orderId = IdUtil.getSnowflakeNextId();
        // 订单信息
        Order order = new Order();
        order.setOrderId(orderId);
        order.setShopId(shopCartOrderDto.getShopId());
        order.setShopName(shopCartOrderDto.getShopName());

        // 用户id
        order.setUserId(userId);
        // 商品总额
        order.setTotal(shopCartOrderDto.getTotal());
        order.setStatus(1);
        order.setIsPayed((byte) 0);
        order.setDeleteStatus(0);
        order.setAllCount(shopCartOrderDto.getTotalCount());
        order.setDeliveryType(dvyType);
        return order;
    }

}
